<template>
  <div>
    <to-search></to-search>
    <div class="con">
      <!-- 左侧 -->
      <div class="left">
        <div
          v-for="(item, index) in arr"
          :key="item.id"
          :class="index === n ? 'active' : ''"
          @click="clickleft(index)"
        >
          {{ item.catename }}
        </div>
      </div>
      <!-- 右侧 -->
      <div class="right">
        <!-- 某一个的数据 -->
        <div class="item" ref="item" v-for="item in arr" :key="item.id">
          <div class="h2">{{ item.catename }}</div>
          <div class="content">
            <div class="i" v-for="i in item.children" :key="i.id" @click="$router.push('/list?id='+item.id+'&type=2')">
              <img class="img" :src="$pre + i.img" alt="" />
              <div class="text">{{ i.catename }}</div>
            </div>
            <van-empty v-if="!item.children" description="暂无数据" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reqcate } from "../../http/http";
export default {
  data() {
    return {
      arr: [],
      n: 0, //选中的下标
    };
  },
  methods: {
    clickleft(index) {
      // 实现了左侧的选中
      this.n = index;
      // 实现了右侧的滚动
      this.$refs.item[this.n].scrollIntoView({
        behavior: "smooth",
      });
    },
  },
  mounted() {
    reqcate().then((res) => {
      this.arr = res.data.list;
    });
  },
};
</script>

<style lang="less" scoped>
@import "../../less/index.less";
.con {
  height: calc(100vh - @headerHeight - 54px);
  background-color: #f4f4f4;
  display: flex;
  .left {
    width: 2rem;
    background-color: @white;
    margin-right: @margin10;
    font-size: @font-p;
    div {
      text-align: center;
      line-height: 0.8rem;
    }
    .active {
      color: @primary;
    }
  }
  .right {
    flex: 1;
    height: 100%;
    overflow-y: auto;
    .item {
      // margin: @margin10;
      margin-top: 0.1rem;
      background-color: @white;
    }
    .h2 {
      font-weight: bold;
      line-height: 0.8rem;
    }
    .content {
      overflow: hidden;
      .i {
        float: left;
        width: 33.33%;
        text-align: center;
        img {
          width: 0.8rem;
          height: 0.8rem;
          border-radius: 50%;
        }
        .text {
          font-size: @font-p;
        }
      }
    }
  }
}
</style>